import { _decorator, Component, director, EditBox, instantiate, log, Node, Prefab, Scene } from 'cc';
import { NetworkManager } from '../Global/NetworkManager';
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListReq, IApiRoomListRes } from '../Common';
import DataManager from '../Global/DataManager';
import { EventEnum, SceneEnum } from '../Enum';
import { PlayerManager } from '../UI/PlayerManager';
import { RoomManager } from '../UI/RoomManager';
import EventManager from '../Global/EventManager';
const { ccclass, property } = _decorator;

@ccclass('HallManager')
export class HallManager extends Component {
    @property(Node)
    playerContainer: Node;

    @property(Prefab)
    playerPrefab: Prefab;

    @property(Node)
    roomContainer: Node;
    @property(Prefab)
    roomPrefab: Prefab;

    protected onLoad(): void {
        NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer, this);
        NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRoom, this);
        EventManager.Instance.on(EventEnum.RoomJoin, this.handleJoinRoom, this)
    }
    protected start(): void {

        this.playerContainer.destroyAllChildren();
        this.roomContainer.destroyAllChildren();// 清空测试用的子节点
        this.getPlayers();
        this.getRooms();// 获取房间列表
        director.preloadScene(SceneEnum.Room);//  预加载场景
    }

    protected onDestroy(): void {
        NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer, this);
        NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList, this.renderRoom, this);
        EventManager.Instance.off(EventEnum.RoomJoin, this.handleJoinRoom, this)
    }
    async getPlayers() {
        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList, {
        })
        if (!success) {
            console.error("连接失败" + error);
            return;
        }
        console.log("切换第二场景 res" + res);

        this.renderPlayer(res);
    }
    // 获取房间列表
    async getRooms() {
        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {
        })
        if (!success) {
            console.error("连接失败" + error);
            return;
        }
        console.log("切换第二场景 res" + res);

        this.renderRoom(res);
    }
    // 渲染房间列表
    renderRoom({list}:IApiRoomListRes) {
        for (const c of this.roomContainer.children) {
            c.active = false;
        }

        while (this.roomContainer.children.length < list.length) {
            const node = instantiate(this.roomPrefab)
            node.active = false;
            node.setParent(this.roomContainer);
        }
        console.log("list", list);
        for (let i = 0; i < list.length; i++) {
            const data = list[i];
            const node = this.roomContainer.children[i];
            node.getComponent(RoomManager).init(data)
        }
    }
    renderPlayer({ list }: IApiPlayerListRes) {
        for (const c of this.playerContainer.children) {
            c.active = false;
        }

        while (this.playerContainer.children.length < list.length) {
            const node = instantiate(this.playerPrefab)
            node.active = false;
            node.setParent(this.playerContainer);
        }
        // console.log("list", list);
        for (let i = 0; i < list.length; i++) {
            const data = list[i];
            const node = this.playerContainer.children[i];
            node.getComponent(PlayerManager).init(data)
        }
    }

    async handleCreateRoom() {
        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {
        })
        if (!success) {
            console.error("连接失败" + error);
            return;
        }

        DataManager.Instance.roomInfo = res.room;
        console.log("创建房间  DataManager.Instance.roomInfo" + DataManager.Instance.roomInfo);
        director.loadScene(SceneEnum.Room);//跳转场景
    }

    async handleJoinRoom(rid: number) {
        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin, {
            rid,
        })
        if (!success) {
            console.error("连接失败" + error);
            return;
        }

        DataManager.Instance.roomInfo = res.room;
        console.log("加入房间  DataManager.Instance.roomInfo" + DataManager.Instance.roomInfo);
        director.loadScene(SceneEnum.Room);//跳转场景
    }
}

